package cn.good.yan.controller;

import cn.good.yan.mapper.BillMapper;
import cn.good.yan.pojo.Bill;
import cn.good.yan.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 演示 - 基本调用
 *
 * @author shengren.yan
 * @create 2023-09-20
 */
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillMapper billMapper;

    @Value("${server.port}")
    private String serverPort;


    /**
     * 查询
     */
    @GetMapping("/get/{id}")
    public CommonResult<List<Bill>> list(@PathVariable("id") Long id) {
        List<Bill> byBid = billMapper.findByBid(id);
        if (!byBid.isEmpty()) {
            return new CommonResult<>(200, "成功获取，端口："  + serverPort, byBid);
        } else {
            return new CommonResult(500, "获取失败，端口：" + serverPort, null);
        }
    }

    /**
     * 修改
     */
    @GetMapping("/add/{id}")
    public CommonResult<Bill> add(@PathVariable("id") Long id) {
        List<Bill> byBid = billMapper.findByBid(7L);
        Bill bill = byBid.get(0);
        bill.setBillName("修改" + id);
        int update = billMapper.update(bill);
        if (update > 0) {
            return new CommonResult(200, "修改获取，端口："  + serverPort, bill);
        } else {
            return new CommonResult(500, "修改失败，端口："  + serverPort, null);
        }
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public CommonResult<Bill> add(@RequestBody Bill bill) {
        int update = billMapper.add(bill);
        if (update > 0) {
            return new CommonResult(200, "新增获取，端口："  + serverPort, bill);
        } else {
            return new CommonResult(500, "新增失败，端口："  + serverPort, null);
        }
    }

}

